package javaroke.dao;

import java.sql.Connection;
import java.sql.ResultSet;

import javaroke.modelo.Reserva;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javaroke.excepcion.DAOExcepcion;
import javaroke.util.ConexionBD;

public class ReservaDAO extends BaseDAO {
	
	public Collection<Reserva> buscarPorIdCliente(String IdCliente)
			throws DAOExcepcion {
		String query = "select idreserva, fecha, descripcion from Reserva where IdCliente like ?";
		Collection<Reserva> lista = new ArrayList<Reserva>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + IdCliente + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Reserva vo = new Reserva();
				vo.setIdreserva(rs.getInt("idreserva"));
				vo.setFecha(rs.getString("fecha"));
				vo.setDescripcion(rs.getString("descripcion"));		
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	public Reserva insertar(Reserva vo) throws DAOExcepcion {
		String query = "insert into Reserva (fecha,descripcion,eLog,idsala,idcliente,idlocal) values (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getFecha());
			stmt.setString(2, vo.getDescripcion());
			stmt.setInt(3, vo.getElog());
			stmt.setInt(4, vo.getSala().getIdsala());
			stmt.setInt(5, vo.getCliente().getIdcliente());
			stmt.setInt(6, vo.getLocal().getIdlocal());


			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar la Reserva");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdreserva(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Reserva obtener(int idreserva) throws DAOExcepcion {
		Reserva vo = new Reserva();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idreserva, fecha, descripcion from Reserva where idreserva = ?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idreserva);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdreserva(rs.getInt(1));
				vo.setFecha(rs.getString(2));
				vo.setDescripcion(rs.getString(3));

			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idreserva) throws DAOExcepcion {
		String query = "delete from Reserva WHERE idreserva = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idreserva);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar la Reserva");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Reserva actualizar(Reserva vo) throws DAOExcepcion {
		String query = "update Reserva set fecha = ?, descripcion = ?, idsala = ? , idcliente = ? where idreserva = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getFecha());
			stmt.setString(2, vo.getDescripcion());
			stmt.setInt(3, vo.getSala().getIdsala());
			stmt.setInt(4, vo.getCliente().getIdcliente());
			stmt.setInt(5, vo.getIdreserva());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar la Reserva");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Collection<Reserva> listar() throws DAOExcepcion {
		Collection<Reserva> c = new ArrayList<Reserva>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idreserva,fecha,descripcion from Reserva order by fecha";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Reserva vo = new Reserva();
				vo.setIdreserva(rs.getInt("idreserva"));
				vo.setFecha(rs.getString("fecha"));
				vo.setDescripcion(rs.getString("descripcion"));

				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}
